package com.mjt.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mjt.admin.model.RespDataBean;
import com.mjt.admin.pojo.Goods;
import com.mjt.admin.mapper.GoodsMapper;
import com.mjt.admin.pojo.GoodsType;
import com.mjt.admin.query.GoodsQuery;
import com.mjt.admin.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mjt.admin.service.IGoodsTypeService;
import com.mjt.admin.service.IGoodsUnitService;
import com.mjt.admin.utils.AssertUtil;
import com.mjt.admin.utils.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author 莫某人
 * @since 2021-11-01
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    @Resource
    private IGoodsTypeService goodsTypeService;

    @Resource
    private IGoodsUnitService goodsUnitService;


    @Override
    public RespDataBean<Goods> goodsList(GoodsQuery goodsQuery) {
        IPage<Goods> page=new Page<>(goodsQuery.getPage(),goodsQuery.getLimit());
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.eq("is_del",0);
        if (!StringUtil.isEmpty(goodsQuery.getGoodsName())){
            wrapper.like("name",goodsQuery.getGoodsName());
        }
        Integer typeId = goodsQuery.getTypeId();
        if (typeId!=null){
            //查询子类型的id
            Integer pId = goodsTypeService.getById(typeId).getPId();
            if (pId!=-1){
                if (pId==1) {
                    List<Integer> ids = goodsTypeService.getByPid(typeId).stream().map(t -> t.getId()).collect(Collectors.toList());
                    wrapper.in("type_id", ids);
                }else {
                    wrapper.eq("type_id", goodsQuery.getTypeId());
                }
            }
        }

        page=this.baseMapper.selectPage(page,wrapper);

        List<Goods> goods = page.getRecords().stream().map((g)->{
            g.setTypeName(goodsTypeService.getById(g.getTypeId()).getName());
            g.setUnitName(goodsUnitService.getById(g.getUnit()).getName());
            return g;
        }).collect(Collectors.toList());
        page.setRecords(goods);


        return new RespDataBean<Goods>(0,"查询成功",page.getRecords(),page.getTotal());
    }

    @Override
    public void saveGoods(Goods goods) {
        AssertUtil.isTrue(goods.getTypeId()==null,"商品类别不能为空");
        AssertUtil.isTrue(StringUtil.isEmpty(goods.getName()),"商品名称不能为空");
        AssertUtil.isTrue(StringUtil.isEmpty(goods.getModel()),"型号不能为空");
        AssertUtil.isTrue(goods.getPurchasingPrice()==null,"采购价不能为空");
        AssertUtil.isTrue(goods.getSellingPrice()==null,"销售价不能为空");
        AssertUtil.isTrue(goods.getMinNum()==null,"库存下限不能为空");

        AssertUtil.isTrue(goods.getUnit()==null,"单位不能为空");
        AssertUtil.isTrue(StringUtil.isEmpty(goods.getModel()),"型号不能为空");
        AssertUtil.isTrue(StringUtil.isEmpty(goods.getProducer()),"生产厂商不能为空");

        goods.setCode(UUID.randomUUID().toString());
        goods.setInventoryQuantity(0);
        goods.setState(0);
        goods.setLastPurchasingPrice(0F);
        goods.setIsDel(0);
        AssertUtil.isTrue(!this.save(goods),"添加商品成功！");
    }

    @Override
    public void updateGoods(Goods goods) {
        AssertUtil.isTrue(goods.getTypeId()==null,"商品类别不能为空");
        AssertUtil.isTrue(StringUtil.isEmpty(goods.getName()),"商品名称不能为空");
        AssertUtil.isTrue(StringUtil.isEmpty(goods.getModel()),"型号不能为空");
        AssertUtil.isTrue(goods.getPurchasingPrice()==null,"采购价不能为空");
        AssertUtil.isTrue(goods.getSellingPrice()==null,"销售价不能为空");
        AssertUtil.isTrue(goods.getMinNum()==null,"库存下限不能为空");

        AssertUtil.isTrue(goods.getUnit()==null,"单位不能为空");
        AssertUtil.isTrue(StringUtil.isEmpty(goods.getModel()),"型号不能为空");
        AssertUtil.isTrue(StringUtil.isEmpty(goods.getProducer()),"生产厂商不能为空");

        //goods.setCode(UUID.randomUUID().toString());
        //goods.setInventoryQuantity(0);
        //goods.setState(0);
        //goods.setLastPurchasingPrice(0F);
        //goods.setIsDel(0);
        AssertUtil.isTrue(!this.updateById(goods),"添加商品成功！");

    }

    @Override
    public void deleteGoods(Integer id) {
        Goods goods = this.getById(id);
        goods.setIsDel(1);
        AssertUtil.isTrue(!this.updateById(goods),"删除成功");

    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void updateStock(Goods goods) {
        /**
         * 商品库存量>0
         * 商品成本价>0
         */
        Goods temp =this.getById(goods.getId());
        AssertUtil.isTrue(null == goods,"待更新的商品记录不存在!");
        AssertUtil.isTrue(goods.getInventoryQuantity()<=0,"库存量必须>0");
        AssertUtil.isTrue(goods.getPurchasingPrice()<=0,"成本价必须>0");
        AssertUtil.isTrue(!(this.updateById(goods)),"商品更新失败!");
    }

    @Override
    public void deleteStock(Integer id) {
        Goods temp =this.getById(id);
        AssertUtil.isTrue(null == temp,"待更新的商品记录不存在!");
        AssertUtil.isTrue(temp.getState() == 2,"该商品已经发生单据，不可删除!");
        temp.setInventoryQuantity(0);
        AssertUtil.isTrue(!(this.updateById(temp)),"商品删除失败!");
    }


}
